//
// Created by 张祎鹤严 on 2023/9/24.
//

/**
******************************************************************************
* @file    bsp_usb.c
* @author  XJTU ROBOMASTER Team
* @brief   bsp usb
        *          这个文件提供函数对usb通讯的数据进行发送和接收处理，包括
*           + usb发送数据处理
*           + usb接收数据处理
*
@verbatim
==============================================================================

@endverbatim
******************************************************************************
* @attention
        *
        * USB接收数组的大小可以在cubemx生成时更改，目前为17
        *
        * Please add comments after adding or deleting functions to ensure code
* specification.
*
******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include "bsp_usb.h"

/* ----------------------- Function Implements ---------------------------- */
/**
  * @brief  Update RxData
  * @retval None
  */
void VisionRxData(float* position_x, float* position_y, float* position_z, uint8_t* status)
{
    /*该处为一种浮点数数据传输的尝试，应该是正确的，如果视觉端也这样发送的话是没有问题的*/
//	union DataConversion data;
//	uint8_t i = 0;
//	for (i = 0; i < 4; i++)
//	{
//		data.buffer[i] = UserRxBufferFS[i];
//	}
//	*position_x = data.data;
//	for (i = 4; i < 8; i++)
//	{
//		data.buffer[i] = UserRxBufferFS[i - 4];
//	}
//	*position_y = data.data;
//	for (i = 8; i < 12; i++)
//	{
//		data.buffer[i] = UserRxBufferFS[i - 8];
//	}
//	*position_z = data.data;
//	*status = UserRxBufferFS[12];

    unsigned int hor = 0, ver = 0, z = 0;
    uint8_t m = 0;
    //每将?个uint8_t加进去，就将其左移，使得最低的?位为0000，然后就可以继续把uint8_t加进去
    for (int i = 3; i >= 0; i--)//yaw
    {
        hor += UserRxBufferFS[i];
        if (i != 0)hor = hor << 8;
    }
    //同上
    for (int i = 7; i >= 4; i--)//pitch
    {
        ver += UserRxBufferFS[i];
        if (i != 4)ver = ver << 8;
    }
    for (int i = 11; i >= 8; i--)//ser
    {
        z += UserRxBufferFS[i];
        if (i != 8)z = z << 8;
    }
    m = UserRxBufferFS[12];

    //强制转换
    position_x = ((float*)&hor);
    position_y = ((float*)&ver);
    position_z = ((float*)&z);
    status = ((uint8_t*)&m);
}

/**
  * @brief  Update TxData
  * @retval None
  */
void VisionTxData(float horizontal, float vertical, float velocity, uint8_t status)
{
    uint8_t i = 0;
    unsigned int hor = *((unsigned int*)&(horizontal));
    unsigned int ver = *((unsigned int*)&(vertical));
    unsigned int ser = *((unsigned int*)&(velocity));
    unsigned int m = *((unsigned int*)&(status));
    // 移位，并且位与操作取后 8 位
    for (i = 0; i < 4; i++)
    {
        uint8_t tmp = (hor >> (8 * i)) & 0xff;
        UserTxBufferFS[i] = tmp;
    }//0123
    for (i = 0; i < 4; i++)
    {
        uint8_t tmp = (ver >> (8 * i)) & 0xff;
        UserTxBufferFS[i + 4] = tmp;
    }//4567
    for (i = 0; i < 4; i++)
    {
        uint8_t tmp = (ser >> (8 * i)) & 0xff;
        UserTxBufferFS[i + 8] = tmp;
    }//8901
    for (i = 0; i < 1; i++)
    {
        uint8_t tmp = (m >> (8 * i)) & 0xff;
        UserTxBufferFS[i + 12] = tmp;
    }//12
    CDC_Transmit_FS(UserTxBufferFS, 13);
}

/************************ (C) COPYRIGHT XJTU ROBOMASTER ********END OF FILE****/

